<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
    var wsUri = "wss://bhmall.zhiyesoft.cn/bh-webserver/auction/14959";
    console.log(wsUri);
    var output;

    function init() {
        output = document.getElementById("output");
        testWebSocket();
    }

    function ajax() {
        var ajaxData = {
            type: arguments[0].type || "GET",
            url: arguments[0].url || "",
            async: arguments[0].async || "true",
            data: arguments[0].data || null,
            dataType: arguments[0].dataType || "text",
            contentType: arguments[0].contentType || "application/json",
            beforeSend: arguments[0].beforeSend || function () {},
            success: arguments[0].success || function () {},
            error: arguments[0].error || function () {}
        }
        ajaxData.beforeSend()
        var xhr = createxmlHttpRequest();
        xhr.responseType = ajaxData.dataType;
        xhr.open(ajaxData.type, ajaxData.url, ajaxData.async);
        xhr.setRequestHeader("Content-Type", ajaxData.contentType);
        xhr.send(convertData(ajaxData.data));
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    ajaxData.success(xhr.response)
                } else {
                    ajaxData.error()
                }
            }
        }
    }
	
	setInterval(function(){
		  ajax({
		        type: "POST",
		        url: "https://bhmall.zhiyesoft.cn/bh-product-api/api/dauction/dauctionNotice",
		        dataType: "json",
		        data: JSON.stringify({
		            goodsId: "15794",
		            mId: "14959",
		            auctionPrice: "1"
		        }),
		        beforeSend: function () {
		            //some js code 
		        },
		        success: function (msg) {
		            console.log(msg)
		        },
		        error: function () {
		            console.log("error")
		        }
		    })		
	},3000);

    function createxmlHttpRequest() {
        if (window.ActiveXObject) {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } else if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        }
    }

    function convertData(data) {
        if (typeof data === 'object') {
            var convertResult = "";
            for (var c in data) {
                convertResult += c + "=" + data[c] + "&";
            }
            convertResult = convertResult.substring(0, convertResult.length - 1)
            return convertResult;
        } else {
            return data;
        }
    }

    function testWebSocket() {
        websocket = new WebSocket(wsUri);
        websocket.onmessage = function (evt) {
            onMessage(evt);
						console.log(evt);
        };
    }


    function onMessage(evt) {
        writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
    }


    function writeToScreen(message) {
        var pre = document.createElement("p");
        pre.style.wordWrap = "break-word";
        pre.innerHTML = message;
        output.appendChild(pre);
    }

    window.addEventListener("load", init, false);
</script>
<h2>WebSocket Test</h2>
<div id="output"></div>

</html>
